<?php
include("../mysqlcon.php");
mysql_select_db($dbname,$conn);
mysql_query('SET NAMES utf8');
$inputDate = $_GET['date'];
$reserv_id = $_GET['reserv_id'];
/* echo $reserv_id; */
//$inputDate = "2010-05-03,2010-05-05";
list($bYear, $bMonth, $bDay) = explode("-",substr($inputDate,0,10));
list($eYear, $eMonth, $eDay) = explode("-",substr($inputDate,11,10));

$bDate = mktime(0,0,0,$bMonth,$bDay,$bYear);
$eDate = mktime(0,0,0,$eMonth,$eDay,$eYear);
$reserv_begin = date("Y-m-d", $bDate);
$reserv_end = date("Y-m-d", $eDate);
$roomListArray = Array();
$arr_roomID = Array();
if($reserv_id != ""){
	$sql4 = "SELECT reservation.room_id
				FROM reservation
				WHERE reservation.reserv_gid = '$reserv_id'";
	$r4 = mysql_query($sql4,$conn);
	while ($rs4 = mysql_fetch_array($r4)){
		array_push($roomListArray,$rs4['room_id']);
	}
	//$arr = explode ( ",", $roomListArray );
	foreach($roomListArray as $t){
		//$arr_roomID = trim($t);
		array_push($arr_roomID, trim($t));
	}
	$temp = '';
	$length = sizeof ( $roomListArray );
}

/*$validatesql = "SELECT room_id FROM resort
 WHERE roomtype_id = '$id' AND room_id NOT IN(SELECT DISTINCT reservation.room_id
 FROM reservation WHERE '$reserv_begin' BETWEEN reservation.reserv_begin AND reservation.reserv_end)";*/
//echo '<div style="width:385px">';
$sql = "SELECT * FROM roomtype";
$r = mysql_query($sql,$conn);
$x = 0;
$table = ""; //initial data
$table .= '<table  id="table">';
$table .= '		<thead>';
$table .= '		<tr class="odd">';
$table .= '		<th scope="col" abbr="action">จอง</th>';
$table .= '		<th scope="col" abbr="room_id">หมายเลขห้องพัก</th>';
$table .= '		<th scope="col" abbr="roomsize_name">ขนาดห้อง</th>';
$table .= '		<th scope="col" abbr="roomtype_name">ชนิดห้อง</th>';
$table .= '		<th scope="col" abbr="reserv_etc">รายละเอียด</th>';
$table .= '		<th scope="col" abbr="extra_bed">เตียงเสริม</th>';
$table .= '		</tr>';
$table .= '		</thead>';
$table .= '		<tbody>';
echo $table;

while ($rs = mysql_fetch_array($r))
{
	if($x==sizeof($rs)){
		//echo "<div style='float:left;position:reative;margin-right:100px;'><p><strong>".$rs['roomtype_name']."</strong></p>";
	}
	else{
		//echo "<div style='float:left;position:releative; margin-right:10px;'><p><strong>".$rs['roomtype_name']."</strong></p>";
	}
	$sql2 = "	SELECT room_id, roomsize_name
				FROM resort
				INNER JOIN roomtype ON roomtype.roomtype_id = resort.roomtype_id
				INNER JOIN roomsize ON roomsize.roomsize_id = resort.roomsize_id
				WHERE resort.roomtype_id = ".$rs['roomtype_id']."
				AND resort.roomtype_id = roomtype.roomtype_id
				ORDER BY resort.room_id ASC";
	 
	//$sql2 = "SELECT room_id FROM resort,roomtype WHERE resort.roomtype_id = $rs[roomtype_id] AND room_id NOT IN(SELECT DISTINCT reservation.room_id FROM reservation WHERE reserv_status != '2' AND (`reserv_end` > '$reserv_begin' AND `reserv_end` <= '$reserv_end'
	//OR `reserv_begin` >= '$reserv_begin' AND `reserv_begin` <= '$reserv_end')) AND resort.roomtype_id = roomtype.roomtype_id ORDER BY resort.room_id ASC";
	$r2 = mysql_query($sql2,$conn);
	$i = 0;
	$sql3 = "";
	while ($rs2 = mysql_fetch_array($r2))
	{
		$countTopic++;
		if(!empty($reserv_id)){
			$sql3 = "SELECT DISTINCT reservation.room_id,reservation.reserv_status AS reserv_status
				FROM reservation
				WHERE (reserv_end > '$reserv_begin'
						AND reserv_end < '$reserv_end'
						OR reserv_begin >= '$reserv_begin'
						AND reserv_begin <= '$reserv_end')
					AND room_id LIKE '%".$rs2['room_id']."%'
					AND reserv_gid != '$reserv_id' ORDER BY reservation.room_id ASC";
			$sql5 = "SELECT room_id,reserv_etc,reserv_gid,extra_bed
				FROM reservation
				WHERE reserv_gid = '$reserv_id' AND room_id = '".$rs2['room_id']."'
				LIMIT 1";
		}else{
			$sql3 = "SELECT DISTINCT reservation.room_id,reservation.reserv_status AS reserv_status
			FROM reservation
			WHERE  (reserv_end > '$reserv_begin'
					AND reserv_end < '$reserv_end'
					OR reserv_begin >= '$reserv_begin'
					AND reserv_begin <= '$reserv_end')
				AND room_id LIKE '%".$rs2['room_id']."%' ORDER BY reservation.room_id ASC";
			$sql5 = "";
		}

		$r3 = mysql_query($sql3,$conn);
		$numrow = mysql_num_rows($r3);
		$extraBed = "";
		$reserv_etc = "";
		if($sql5 != ""){
			$r5 = mysql_query($sql5,$conn);
			if(mysql_num_rows($r5) != 0){
				$extraBed = mysql_result ( $r5, 0, 3 );
				$reserv_etc = mysql_result ( $r5, 0, 1 );
			}
		}
/* 		echo $numrow; */
		//$rs_reserv_id = mysql_result ( $r3, $i, 1 );
		/*if($numrow == 0){
		echo "<input type='checkbox' name='roomlist_$rs2[room_id]' id='roomlist_$rs2[room_id]' value='$rs2[room_id]' />$rs2[room_id]<br>";
		}else{
		if($reserv_id != ""){
		echo "<input type='checkbox' name='roomlist_$rs2[room_id]' id='roomlist_$rs2[room_id]' value='$rs2[room_id]' checked = 'yes' />$rs2[room_id]<br>";
		}
		}*/
		$rs_status = mysql_fetch_array($r3);
		$hilight = "";
		if($numrow != 0){
			if($rs_status['reserv_status'] < 2){
				$hilight = "style='color:red;'";
			}else{
				continue;
			}
		}
		if($countTopic%2==0)
		echo '<tr class="odd">';
		else
		echo '<tr class="even">';
		if(!empty($reserv_id)){
/* 			if($numrow == 0){ */
				if(in_array (trim($rs2['room_id']), $arr_roomID)){
					echo "<td>";
					if($hilight == ""){
					echo "<input type='checkbox' name='roomlist_".$rs2['room_id']."' id='roomlist_".$rs2['room_id']."' value='".$rs2['room_id']."' checked = 'yes' />";}
					echo "</td>";
					echo "<td $hilight>" .$rs2['room_id']. "</td>";
					echo "<td $hilight>" .$rs2['roomsize_name']. "</td>";
					echo "<td $hilight>" .$rs['roomtype_name']. "</td>";
					echo "<td><input type='text' name='reserv_etc_".$rs2['room_id']."' id='reserv_etc_".$rs2['room_id']."' value='".$reserv_etc."'></input></td>";
					echo '<td><div class="exBed">';
					echo '	<select name="extraBed_'.$rs2['room_id'].'" id="extraBed" style="width: 40px">';
					echo '		<option value="0"> - </option>';
					for($i = 1; $i <= 10; $i++){
						$selected = $extraBed == $i ? "selected":"";
						echo '<option value="' . $i . '" '.$selected.'>' . $i . '</option>';
					}
					echo '	</select>';
					echo '</div></td>';
					//echo "<td>" .$rs['roomtype_name']. "</td>";
				}else{
					echo "<td>";
					if($hilight == ""){
					echo "<input type='checkbox' name='roomlist_".$rs2['room_id']."' id='roomlist_".$rs2['room_id']."' value='".$rs2['room_id']."' />";}
					echo "</td>";
					echo "<td $hilight>" .$rs2['room_id']. "</td>";
					echo "<td $hilight>" .$rs2['roomsize_name']. "</td>";
					echo "<td $hilight>" .$rs['roomtype_name']. "</td>";
					echo "<td><input type='text' name='reserv_etc_".$rs2['room_id']."' id='reserv_etc_".$rs2['room_id']."' value='".$reserv_etc."'></input></td>";
					echo '<td><div class="exBed">';
					echo '	<select name="extraBed_'.$rs2['room_id'].'" id="extraBed" style="width: 40px">';
					echo '		<option value="0"> - </option>';
					for($i = 1; $i <= 10; $i++){
						$selected = $extraBed == $i ? "selected":"";
						echo '<option value="' . $i . '" '.$selected.'>' . $i . '</option>';
					}
					echo '	</select>';
					echo '</div></td>';
					//echo "<td>" .$rs['roomtype_name']. "</td>";
				}
/* 			} */
		}else{
/* 			if($numrow == 0){ */
				echo "<td>";
				if($hilight == ""){echo "<input type='checkbox' name='roomlist_".$rs2['room_id']."' id='roomlist_".$rs2['room_id']."' value='".$rs2['room_id']."' />";}
				echo "</td>";
				echo "<td $hilight>" .$rs2['room_id']. "</td>";
				echo "<td $hilight>" .$rs2['roomsize_name']. "</td>";
				echo "<td $hilight>" .$rs['roomtype_name']. "</td>";
				echo "<td><input type='text' name='reserv_etc_".$rs2['room_id']."' id='reserv_etc_".$rs2['room_id']."' value='".$reserv_etc."'></input></td>";
				echo '<td><div class="exBed">';
				echo '	<select name="extraBed_'.$rs2['room_id'].'" id="extraBed" style="width: 40px">';
				echo '		<option value="0"> - </option>';
				for($i = 1; $i <= 10; $i++){
					$selected = $extraBed == $i ? "selected":"";
					echo '<option value="' . $i . '" '.$selected.'>' . $i . '</option>';
				}
				echo '	</select>';
				echo '</div></td>';
				//echo "<td>" .$rs['roomtype_name']. "</td>";
/* 			} */
		}
		$i++;
	}
	//echo "</div>";
	$x++;
}
echo 		"</tbody>";
echo "</table>";

//echo "</div>";
?>
